import {_decorator, Animation, AnimationClip, AnimationState, Component, director, macro, resources} from "cc";
import {FishDoSomethingEvent} from "db://assets/script/common/events/fish-events/fish-do-something-event";
import {FishInPool} from "db://assets/script/game/fish/fish-in-pool";
import {GameEvent} from "db://assets/script/game-event";
import {FishStatus} from "db://assets/proto/shared/v1/biz";
import {userInfo} from "db://assets/script/user-info";
import Long from "long";
import {FishGameStatus} from "db://assets/script/game/constants/fish-game-status";
import {FishAttackRecordReply} from "db://assets/proto/new-world-ws/v1/internal";

const {ccclass, property} = _decorator;

@ccclass('FishShow')
export class FishShow extends Component {

    fishId!: Long;
    roleType: number = 1
    animation!: Animation

    attackEvents: FishAttackRecordReply[] = []


    init(fishId: Long, roleType: number) {
        const topic = GameEvent.fishStatusEventPrefix + fishId;
        director.on(topic, this.handle, this)
        this.fishId = fishId;
        this.roleType = roleType;
        this.animation = this.getComponent(Animation)!
        this._beginStatusChangeEvent()
        this.initAnimation()
    }

    start() {
    }

    onLoad() {

    }

    initAnimation() {
        this.animation.getState(this.walkName()).repeatCount = macro.REPEAT_FOREVER
        this.animation.getState(this.idleName()).repeatCount = macro.REPEAT_FOREVER
        this.animation.getState(this.runningName()).repeatCount = macro.REPEAT_FOREVER
        this.animation.getState(this.deathName()).repeatCount = 1
        this.animation.getState(this.attackName()).repeatCount = 1
        this.animation.getState(this.digName()).repeatCount = 3
    }

    notificationPlay(event: FishDoSomethingEvent) {
        director.emit(GameEvent.fishStatusEventPrefix + this.fishId, event)
    }

    handle(event: FishDoSomethingEvent) {
        if (!this || !this.node.isValid || event.fishStatusLoader() == FishGameStatus.DEAD) return
        const currentAction = this.node.parent!.getComponent(FishInPool)!.gameAction
        console.log(this.fishId, event, 'current action:', currentAction, 'new action:', event.gameAction);
        if (currentAction < event.gameAction) {
            this.animation!.stop()
            this.animation!.once(Animation.EventType.FINISHED, event.animationCallback)
            this.animation.getState(event.backgroundAnimationName).duration = event.animationDuration * 0.9 / 1000
            this.animation!.play(event.backgroundAnimationName)
            console.log(this.fishId, 'do current action:', currentAction, 'new action:', event.gameAction, 'will play:', event.backgroundAnimationName, 'repeat count: ', event.backgroundAnimationRepeatCount)
            this.node.parent!.getComponent(FishInPool)!.gameAction = event.gameAction
            const mt = event.moveTo()
            this.node.parent!.getComponent(FishInPool)!.moveTo(mt.x, mt.y, event.moveDuration, event.moveCallback)
        }
    }

    idleName(): string {
        console.log('try load idle: ', 'idle-' + this.roleType)
        return 'idle-' + this.roleType
    }

    walkName(): string {
        console.log('try load walk: ', 'walk-' + this.roleType)
        return 'walk-' + this.roleType
    }

    runningName(): string {
        console.log('try load running: ', 'running-' + this.roleType)
        return 'running-' + this.roleType
    }

    digName(): string {
        console.log('try load dig: ', 'dig-' + this.roleType)
        return 'dig-' + this.roleType
    }

    attackName(): string {
        console.log('try load attack: ', 'attack-' + this.roleType)
        return 'attack-' + this.roleType
    }

    deathName(): string {
        console.log('try load death: ', 'death-' + this.roleType)
        return 'death-' + this.roleType
    }


    _beginStatusChangeEvent() {
        let willPlay = this.idleName()
        const ff = userInfo.fishInfo.loadFishById(this.fishId)
        if (ff) {
            if (ff!.status == FishStatus.dead) {
                willPlay = this.deathName()
            }
            this.animation.play(willPlay)
        }
    }

    nearAttackEvent() {
        if (this.attackEvents.length > 0) {
            director.emit(GameEvent.fishBeginBeAttack, this.attackEvents.shift())
        }
    }

    onFinished() {

    }

    protected onDestroy() {
        director.off(GameEvent.fishStatusEventPrefix + this.fishId, this.handle, this)
    }


}